import { PrismaService } from '@app/prisma';
import { RandomNameService } from '@app/utils/random-name/random-name.service';
import { Controller, Get, Query } from '@nestjs/common';
import { StudentService } from 'src/common/student/student.service';
import { TeacherService } from 'src/common/teacher/teacher.service';
import { TaskService } from 'src/task/task.service';
import { TestService } from './test.service';

@Controller('test')
export class TestController {
    constructor(
        private readonly studentService: StudentService,
        private readonly randomNameService: RandomNameService,
        private readonly prisma: PrismaService,
        private readonly teacherService: TeacherService,
        private readonly taskService: TaskService,
        private readonly testService: TestService
    ) { }

    @Get("randomTeacher")
    async randomTeacher(
        @Query("count") count: string
    ) {
        const total = Number(count);
        const classes = await this.prisma.classes.findMany();
        for (let i = 0; i < total; i++) {
            const name = this.randomNameService.getName(false);
            const teacher = await this.teacherService.findOrCreateTeacher(name);
            for (const item of classes) {
                await this.teacherService.findOrCreateClassesOnTeacher(item, teacher)
            }
        }
    }

    @Get("allPickTask")
    async allPickTask(
        @Query("id") id: string
    ) {
        const taskId = Number(id);
        this.testService.allin(taskId);
        return "loading"
    }

    @Get("randomUser")
    async randomUser(
        @Query("college") college: string,
        @Query("name") name: string,
        @Query("department") department: string,
        @Query("count") count: string
    ) {
        const coll = await this.studentService.findOrCreateCollege(college);
        const dept = await this.studentService.findOrCreateDepartmentToCollege(
            coll.id,
            department
        )
        const science = await this.studentService.findOrCreateScience(
            dept.id,
            name
        );
        const total = Number(count);
        const str = "123456789qwertyuiiopasdfghjjklzxcvbnm"
        for (let i = 0; i < total; i++) {
            const memberLength = Math.floor((Math.random() * 10 + 40));
            const classesName = `${college}${department}${this.randomNameService.getNickHeader()}`
            const classess = await this.studentService.findOrCreateClasses(
                science.id,
                classesName
            )
            for (let j = 0; j < memberLength; j++) {
                const userName = this.randomNameService.getName(false);
                await this.studentService.findOrCreateUser(
                    userName,
                    new Array(13).fill(0).map(e => {
                        return str[Math.floor(Math.random() * str.length)]
                    }).join(""),
                    classess.id
                )
            }
        }
    }
}
